package com.fangcun.service.admin.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fangcun.bean.admin.user.response.RolePermission;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.entity.AdminMenuPermission;
import com.fangcun.entity.AdminRole;
import com.fangcun.entity.AdminRolePermission;
import com.fangcun.entity.AdminUserRole;
import com.fangcun.mapper.AdminMenuPermissionMapper;
import com.fangcun.mapper.AdminRoleMapper;
import com.fangcun.mapper.AdminRolePermissionMapper;
import com.fangcun.mapper.AdminUserRoleMapper;
import com.fangcun.service.admin.PermissionService;

/**
 * @作者： 陳曉鬆 @时间： 2019年2月25日 - 上午10:18:36 @description：->
 */
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private AdminMenuPermissionMapper menuMapper;

	@Autowired
	private AdminRoleMapper roleMapper;

	@Autowired
	private AdminRolePermissionMapper rolePermissionMapper;

	@Autowired
	private AdminUserRoleMapper userRoleMapper;

	@Override
	public List<AdminMenuPermission> getMenuPermissionList(AdminMenuPermission menu) throws ProException {
		List<AdminMenuPermission> list = menuMapper.selectbyList(menu);
		if (list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	@Transactional
	public Long insertMenuPermission(AdminMenuPermission menu) throws ProAffairException {
		if (menuMapper.insertSelective(menu) == 0 || menu.getId() == null) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		return menu.getId();
	}

	@Override
	public void updateMenuPermission(AdminMenuPermission menu) throws ProAffairException {
		if (menuMapper.updateByPrimaryKeySelective(menu) == 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void deleteMenuPermission(Long id) throws ProAffairException {
		AdminMenuPermission record = new AdminMenuPermission();
		record.setParentId(id);
		List<AdminMenuPermission> list = menuMapper.selectbyList(record);
		if (list != null && list.size() > 0) {
			for (AdminMenuPermission a : list) {
				if (a.getChildList() != null && a.getChildList().size() > 0) {
					for (AdminMenuPermission a1 : a.getChildList()) {
						if (a1.getChildList() != null && a1.getChildList().size() > 0) {
							for (AdminMenuPermission a2 : a1.getChildList()) {
								menuMapper.deleteByPrimaryKey(a2.getId());
							}
						}
						menuMapper.deleteByPrimaryKey(a1.getId());
					}
				}
				menuMapper.deleteByPrimaryKey(a.getId());
			}
		}
		if (menuMapper.deleteByPrimaryKey(id) == 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public List<AdminRole> getRoleList(AdminRole role) throws ProAffairException {
		List<AdminRole> list = roleMapper.getRoleList(role);
		if (list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public Long insertRole(AdminRole role) throws ProAffairException {
		if (roleMapper.insertSelective(role) == 0 || role.getId() == null) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		return role.getId();
	}

	@Override
	public void updateRole(AdminRole role) throws ProAffairException {
		if (roleMapper.updateByPrimaryKeySelective(role) == 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void deleteRole(Long id) throws ProAffairException {
		if (userRoleMapper.selectByRoleId(id).size() > 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "有管理员使用该角色,不能删除");
		}
		rolePermissionMapper.deleteByRoleId(id);
		if (roleMapper.deleteByPrimaryKey(id) == 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void updateUserRole(AdminUserRole userRole) throws ProAffairException {
		if (userRoleMapper.updateByUserId(userRole) == 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public List<RolePermission> getRolePermissionList(Long roleId) throws ProAffairException {
		Map<String, Object> map = new HashMap<>();
		map.put("type", 0);
		map.put("roleId", roleId);
		List<RolePermission> list = rolePermissionMapper.getRolePermissionList(map);
		if (list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public void insertRolePermission(List<AdminRolePermission> rp) throws ProAffairException {
		if (rp.size() > 0) {
			if (!StringUtils.isEmpty(rp.get(0).getRoleId())) {
				AdminRole role = roleMapper.selectByPrimaryKey(rp.get(0).getRoleId());
				if (role == null) {
					throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), "角色不存在");
				}

				rolePermissionMapper.deleteByRoleId(rp.get(0).getRoleId());

				List<AdminRolePermission> add = new ArrayList<AdminRolePermission>();

				List<Long> ids = new ArrayList<>();
				for (AdminRolePermission record : rp) {
					if (!add.contains(record)) {
						ids.add(record.getMenuId());
						add.add(record);
					}
				}
				// 得到父级ids
				for (Long l : rolePermissionMapper.getParentIds(ids)) {
					AdminRolePermission a = new AdminRolePermission();
					a.setRoleId(rp.get(0).getRoleId());
					a.setMenuId(l);
					if (!add.contains(a)) {
						add.add(a);
					}
				}
				// 添加
				for (AdminRolePermission record : add) {
					if (record.getMenuId() != 0) {
						try {
							rolePermissionMapper.insertSelective(record);
						} catch (Exception e) {
						}
					}
				}
			}
		}
	}
}
